home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapte19 / palette.c < prev    next >
C/C++ Source or Header  |  1996-04-29  |  7KB  |  245 lines

  1.  
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include "Palette.h"
  5. #include <vfw.h>
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "My Application"; 
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.     WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107.  
  108. #define MSG_LEN          1024
  109.  
  110. char msg[MSG_LEN+1];
  111. HWND hMCIWnd = NULL;   // MCIWnd window handle
  112.  
  113.  
  114. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  115. {
  116.    switch( uMsg )
  117.    {
  118.       case WM_CREATE :
  119.               // create MCIWnd window
  120.               //.....................
  121.  
  122.               hMCIWnd = MCIWndCreate(hWnd, hInst, 
  123.                                      WS_VISIBLE | 
  124.                                      WS_CHILD | 
  125.                                      WS_OVERLAPPED |
  126.                                      WS_CAPTION |
  127.                                      WS_BORDER |
  128.                                      MCIWNDF_NOTIFYMODE |
  129.                                      MCIWNDF_SHOWALL, 
  130.                                      "Sample.avi"
  131.                                     );
  132.  
  133.               MCIWndSetOwner(hMCIWnd, hWnd);
  134.                           
  135.               if (!hMCIWnd)
  136.               {
  137.                   MessageBox(hWnd, "Error creating MCIWnd window...", 
  138.                              NULL, MB_OK);
  139.                   DestroyWindow( hWnd );
  140.               }
  141.               break;
  142.  
  143.       case WM_COMMAND :
  144.               switch( LOWORD( wParam ) )
  145.               {
  146.                  case IDM_SET_PALETTE :
  147.                         {
  148.                           HPALETTE hPalette;
  149.  
  150.                           hPalette = MCIWndGetPalette(hMCIWnd);
  151.                           
  152.                           // make palette modifications here
  153.                           //      .
  154.                           //      .
  155.                           //      .
  156.  
  157.                           MCIWndSetPalette(hMCIWnd, hPalette);
  158.                         }
  159.                         break;
  160.  
  161.                  case IDM_ABOUT :
  162.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  163.                         break;
  164.  
  165.                  case IDM_EXIT :
  166.                         DestroyWindow(hWnd);
  167.                         break;
  168.               }
  169.               break;
  170.  
  171.       // realize palette when window receives focus
  172.       //...........................................
  173.       
  174.       case WM_SETFOCUS :
  175.               MCIWndRealize(hMCIWnd, FALSE);
  176.               break;
  177.  
  178.       case WM_ACTIVATE :
  179.               if ( hMCIWnd && (LOWORD(wParam) & WA_ACTIVE ||
  180.                                LOWORD(wParam) & WA_CLICKACTIVE) )
  181.               {
  182.                   MCIWndRealize(hMCIWnd, FALSE);
  183.               }
  184.               break;
  185.  
  186.       // MCIWnd error notification
  187.       //..........................
  188.  
  189.       case MCIWNDM_NOTIFYERROR :
  190.               {
  191.                  MCIWndGetError(hMCIWnd, msg, MSG_LEN);
  192.                  MessageBox(hWnd, msg, "MCIWnd Error", MB_OK);
  193.               }
  194.               break;
  195.  
  196.       case WM_DESTROY :
  197.               // destroy MCIWnd, if one exists
  198.               //..............................
  199.  
  200.               if (hMCIWnd)
  201.                   MCIWndDestroy(hMCIWnd);
  202.  
  203.               PostQuitMessage(0);
  204.               break;
  205.  
  206.       default :
  207.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  208.    }
  209.  
  210.    return( 0L );               
  211. }
  212.  
  213.  
  214. LRESULT CALLBACK About( HWND hDlg,           
  215.                         UINT message,        
  216.                         WPARAM wParam,       
  217.                         LPARAM lParam)
  218. {
  219.    switch (message) 
  220.    {
  221.        case WM_INITDIALOG: 
  222.                return (TRUE);
  223.  
  224.        case WM_COMMAND:                              
  225.                if (   LOWORD(wParam) == IDOK         
  226.                    || LOWORD(wParam) == IDCANCEL)    
  227.                {
  228.                        EndDialog(hDlg, TRUE);        
  229.                        return (TRUE);
  230.                }
  231.                break;
  232.    }
  233.  
  234.    return (FALSE); 
  235. }
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.